﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _10进度条
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker1_ProgressChanged);
            this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorker1_RunWorkerCompleted);
        }
        /// <summary>
        /// 定义一个代理，用于更新ProgressBar的值（Value）及在执行方法的时候，返回方法的处理信息。
        /// </summary>
        /// <param name="ipos"></param>
        /// <param name="vinfo"></param>
        private delegate void SetPos(int ipos, string vinfo);//代理
        /// <summary>
        /// 进度条值更新函数（参数必须跟声明的代理参数一样）
        /// </summary>
        /// <param name="ipos"></param>
        /// <param name="vinfo"></param>
        private void SetTextMessage(int ipos, string vinfo)
        {
            if (this.InvokeRequired)
            {
                SetPos setpos = SetTextMessage;
                this.Invoke(setpos, new object[] { ipos, vinfo });
            }
            else
            {
                this.label1.Text = ipos.ToString() + "/1000";
                this.progressBar1.Value = Convert.ToInt32(ipos);
                this.textBox1.AppendText(vinfo);
            }
        }
        /// <summary>
        ///  新的线程执行函数
        /// </summary>
        private void SleepT()
        {
            for (int i = 0; i < 500; i++)
            {
                System.Threading.Thread.Sleep(10);
                SetTextMessage(100 * i / 500, i.ToString() + "\r\n");
            }
        }
        /// <summary>
        /// 函数实现
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender, EventArgs e)
        {

            //#region progressBar进度条
            //Thread fThread = new Thread(new ThreadStart(SleepT));
            //fThread.Start();
            //#endregion
            #region backgroundWorker进度条
            this.backgroundWorker1.RunWorkerAsync(); // 运行 backgroundWorker 组件
            //ProcessForm form = new ProcessForm(this.backgroundWorker1);// 显示进度条窗体
            //form.ShowDialog(this);
            //form.Close();


            #endregion
        }      

        private void BackgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
            }
            else
            {
            }
            this.Close();//执行完之后，直接关闭页面
        }
        // 你可以在这个方法内，实现你的调用，方法等。
        private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(100);
                worker.ReportProgress(i);
                if (worker.CancellationPending)  // 如果用户取消则跳出处理数据代码 
                {
                    e.Cancel = true;
                    break;
                }
            }
        }

        private void BackgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.progressBar1.Value = e.ProgressPercentage;
        }
    }
}
